#region imported Namespaces
using System;
using System.Data;
using System.Data.SqlClient;

using BO = PHSRAG.Insight21.BusinessObjects;
using Utility = PHSRAG.Utility;
#endregion

namespace PHSRAG.Insight21.Purchasing
{
	/// <summary>
	/// Business class for PO/AP Details Page. 
	/// It supports loading of OrderHeader, OrderDetail and Order 
	/// Distribution information which the user has access to.
	/// </summary>
	public class POAPDetails: BO.BusinessObject
	{		
		#region Constants (Private)
		private const string POAPSPName		= "GetPOAPDetails";
		private const string DomainUserId	= "@domainUserId";
		private const string POAPNumber		= "@orderNumber";
		private const string POAPType		= "@orderType";
		private const string PODetailsKey	= "PODetailsKey_89767dd9-0bf6-405f-b621-eae5f9d58ded";
		//Used in SendByEmail Functionality
		private const string InsertPageQuerySPName	= "InsertPageQuery";
		private const string InsertQuery			= "@query";
		#endregion
		
		#region Enum StoredPROCArguments
		/// <summary>
		/// The StoredPROCArguments enumeration defines values 
		/// for indexing the stored procedure arguments 
		/// and total number of arguments passed to the 
		/// Stored procedure
		/// </summary>
		private enum StoredPROCArguments 
		{
			DomainUserId	= 0,
			orderNumber		= 1,
			orderType		= 2,
			//Total number of arguments passed to the stored procedure
			TotalArguments			= 3
		}
		/// <summary>
		/// The DataTables enumeration defines values 
		/// for indexing the datatables.
		///</summary>
		private enum DataTables 
		{
			OrderHeader		= 0,
			OrderLine		= 1,
			OrderDistrib	= 2,
			Totals			= 3
		}
		/// <summary>
		/// The DataColumns enumeration defines values 
		/// for indexing the dataColumns.
		///</summary>
		private enum DataColumns 
		{
			BusinessUnit		= 0,
			OrderNumber			= 1,
			VendorName			= 2,
			RequisitionNumber	= 3,
			Requestor			= 4,
              ShipTo            = 5 ,
			OrderDate			= 6,
			OrderStatus			= 7,
			ReceiptStatus		= 8,
			BackorderStatus		= 9
           
		}
		#endregion
		
		#region Instance Properties
		/// <summary>
		/// Get the key to be used in all caching operations for this instance of the object.
		/// </summary>
		public static string CacheKey	{ get { return PODetailsKey; } }
		/// <summary>
		/// Get the Business Unit Name from the OrderHeader datatable which contains only one row.
		/// Returns an empty string if no Business Unit is associated with the OrderNumber.
		/// </summary>
		public string BusinessUnit
		{
			get
			{
				object o = DataContainer.Tables[Constants.POAPDetailsHeaderDataTableName]
					.Rows[0] [(int)DataColumns.BusinessUnit];
				return (o == DBNull.Value) ? string.Empty : (string)o;
			}
		}

		/// <summary>
		/// Get the Order Number (PO/AP) from the OrderHeader datatable which contains only one row.
		/// Returns an empty string if no OrderNumber is available.
		/// </summary>
		public string OrderNumber
		{
			get
			{
				DataTable poDetails = DataContainer.Tables[Constants.POAPDetailsHeaderDataTableName];
				object o = poDetails.Rows.Count > 0 ?poDetails.Rows[0] [(int)DataColumns.OrderNumber]:null;
				return (o == DBNull.Value) ?  string.Empty : (string)o;
			}
		}

		/// <summary>
		/// Get the Vendor Name from the OrderHeader datatable which contains only one row.
		/// Returns an empty string if no Vendor Name is associated with the OrderNumber.
		/// </summary>
		public string VendorName
		{
			get
			{
				object o = DataContainer.Tables[Constants.POAPDetailsHeaderDataTableName]
					.Rows[0][(int)DataColumns.VendorName];
				return (o == DBNull.Value) ?  string.Empty : (string)o;
			}
		}
		/// <summary>
		/// Get the Requisition Number from the OrderHeader datatable which contains only one row.
		/// Returns an empty string if no RequisitionNumber is associated with the OrderNumber.
		/// </summary>
		public string RequisitionNumber
		{
			get
			{
				object o = DataContainer.Tables[Constants.POAPDetailsHeaderDataTableName]
					.Rows[0][(int)DataColumns.RequisitionNumber];
				return (o == DBNull.Value) ?  string.Empty : (string)o;
			}
		}
		/// <summary>
		/// Get the Requestor Name from the OrderHeader datatable which contains only one row.
		/// Returns an empty string if no Requestor Name is associated with the OrderNumber.
		/// </summary>
		public string Requestor
		{
			get
			{
				object o = DataContainer.Tables[Constants.POAPDetailsHeaderDataTableName]
					.Rows[0][(int)DataColumns.Requestor];
				return (o == DBNull.Value) ?  string.Empty : (string)o;
			}
		}

        public string  ShipTo
        {
            get
            {
                object o = DataContainer.Tables[Constants.POAPDetailsHeaderDataTableName]
                    .Rows[0][(int)DataColumns.ShipTo];
                return (o == DBNull.Value) ? string.Empty : (string)o;
            }
        }
		/// <summary>
		/// Get the Order date from the OrderHeader datatable which contains only one row.
		/// Returns an empty string if no OrderDate is associated with the OrderNumber.
		/// </summary>
		public string OrderDate
		{
			get
			{
				object o = DataContainer.Tables[Constants.POAPDetailsHeaderDataTableName]
					.Rows[0][(int)DataColumns.OrderDate];
				return (o == DBNull.Value) ? string.Empty : ((DateTime)o).ToString("MM/dd/yyyy");
			}
		}
		/// <summary>
		/// Get the Order Status from the OrderHeader datatable which contains only one row.
		/// Returns an empty string if no Order Status is associated with the OrderNumber.
		/// </summary>
		public string OrderStatus
		{
			get
			{
				object o = DataContainer.Tables[Constants.POAPDetailsHeaderDataTableName]
					.Rows[0][(int)DataColumns.OrderStatus];
				return (o == DBNull.Value) ?  string.Empty : (string)o;
			}
		}
		/// <summary>
		/// Get the Receipt Status from the OrderHeader datatable which contains only one row
		/// Returns an empty string if no Receipt Status is associated with the OrderNumber.
		/// </summary>
		public string ReceiptStatus
		{
			get
			{
				object o = DataContainer.Tables[Constants.POAPDetailsHeaderDataTableName]
					.Rows[0][(int)DataColumns.ReceiptStatus];
				return (o == DBNull.Value) ? string.Empty : (string)o;
			}
		}
		/// <summary>
		/// Get Back Order Status from the OrderHeader datatable which contains only one row
		/// Returns an empty string if no Back Order Status is associated with the OrderNumber.
		/// </summary>
		public string BackorderStatus
		{
			get
			{
				object o = DataContainer.Tables[Constants.POAPDetailsHeaderDataTableName]
					.Rows[0][(int)DataColumns.BackorderStatus];
				return (o == DBNull.Value) ?  string.Empty : (string)o;
			}
		}
		
		#endregion

		#region Class Methods
		/// <summary>
		/// This method is used in SendByEmail functionality. 
		/// Inserts the PO/AP details information and the method returns the last inserted ID 
		/// </summary>
		/// <returns>last inserted id </returns>
		public static int GetQueryID(string query )
		{
			return Convert.ToInt32(Utility.DataAccess.ExecuteScalar(Utility.DataAccess.GetConnectionString(),InsertPageQuerySPName,
				Utility.DataAccess.BuildSqlParameterArray(InsertQuery,SqlDbType.VarChar,query)));
		}
		#endregion
		
		#region Constructor
		/// <summary>
		/// Initializes the base class with the specified cache policy.
		/// </summary>
		public POAPDetails() : base(null)
		{
			
		}
		#endregion

		#region Business Object Methods

		/// <summary>
		/// Load the PO/AP data for the current user. 
		/// Intialize the DataContainer
		/// The method expects 3 paramters
		/// a) Domain User Id
		/// b) Order Number
		/// c) Order Type.
		/// Throws an exception if an error is encountered during the load process.		
		/// </summary>
		/// <param name="connectionString">database connection string</param>
		/// <param name="args">arguments to load the user order records</param>
		
		public override void Load(string connectionString, params object[] args)
		{
			if (args.Length != (int)StoredPROCArguments.TotalArguments)
				throw new Exception(String.Format("{0} {1} {2}", "PO Details load requires ",
					StoredPROCArguments.TotalArguments.ToString(), "  parameters"));
			try 
			{
				DataContainer = Utility.DataAccess.GetDataSet(
					Utility.DataAccess.GetConnectionString()
					,POAPSPName
					,Utility.DataAccess.BuildSqlParameterArray(
					DomainUserId, SqlDbType.Int, args[(int)StoredPROCArguments.DomainUserId]
					,POAPNumber, SqlDbType.VarChar, args[(int)StoredPROCArguments.orderNumber]	
					,POAPType, SqlDbType.Int, args[(int)StoredPROCArguments.orderType])
					);
				if(DataContainer.Tables.Count != 4)
					throw new Exception("No Data found for the user");
				DataContainer.Tables[(int)DataTables.OrderHeader].TableName = Constants.POAPDetailsHeaderDataTableName;
				DataContainer.Tables[(int)DataTables.OrderLine].TableName = Constants.POAPDetailsDataTableName;
				DataContainer.Tables[(int)DataTables.OrderDistrib].TableName = Constants.POAPDetailsDistribDataTableName;					
				DataContainer.Tables[(int)DataTables.Totals].TableName = Constants.POAPDetailsTotals;						
			}
			catch (Exception ex)
			{
				throw(new Exception(String.Format("{0} Error occurred while loading PO/AP Details.",Utility.ExceptionReport.GetExceptionInfo(ex, false))));
			}

		}
		#endregion		
	}
}
